package com.tianji.learning.Task;

import cn.hutool.core.date.DateTime;
import com.tianji.learning.constant.RedisConstant;
import com.tianji.learning.domain.po.PointsBoard;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.tianji.learning.service.IPointsBoardService;
import com.tianji.learning.utils.TableInfoContext;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class TaskForNewTable {
    private final IPointsBoardSeasonService seasonService;
    private final IPointsBoardService pointsBoardService;
    private final RedisTemplate redisTemplate;

    @XxlJob("createTableJob")
    public void taskForNewTable() {
        log.info("创建了一张新表");
        //1.获取上个月的今天的时间
        LocalDate format = LocalDate.now().minusMonths(1);
        System.out.println(format);
        //2.查询赛季id
        PointsBoardSeason one = seasonService.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, format)
                .ge(PointsBoardSeason::getEndTime, format)
                .one();
        if(one == null) {
            throw new IllegalArgumentException("赛季不存在");
        }
        log.info(one.toString());
        Integer SeasonId = one.getId();
        System.out.println(SeasonId);
        //3.创建新表
        pointsBoardService.createNewTable(SeasonId);
    }

    @XxlJob("savePointsBoard2DB")
    public void savePointsBoard2DB(){
        //1.先获取上个月该天的日期时间
        LocalDate format = LocalDate.now().minusMonths(1);
        //2.查询赛季id
        PointsBoardSeason one = seasonService.lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, format)
                .ge(PointsBoardSeason::getEndTime, format)
                .one();
        if(one == null) {
            throw new IllegalArgumentException("赛季不存在");
        }
        Integer SeasonId = one.getId();
        //3.组合key
        String key = RedisConstant.POINTS_BOARD_KEY_PREFIX + format.toString();
        //4.从redis中取出数据 - 采取分页查询
        //计算分页参数
        int index = XxlJobHelper.getShardIndex(); //分片索引
        int total = XxlJobHelper.getShardTotal(); //分片总数
        int pageNo = index + 1; //起始页
        int pageSize = 1000; //页的大小
        //修改要操作的数据库的名字 - 往ThreadLocal中存即可
        TableInfoContext.setInfo("points_board"+SeasonId);
        //调用pointsBoardService的方法查询该页的数据
        while(true){
            List<PointsBoard> data =
                    pointsBoardService.
                            getCurrentSeasonPointsAndRank(key, pageNo, pageSize);
            if(data == null || data.isEmpty()) {
                //说明数据已经查完了
                log.info("数据已经查完了");
                break;
            }
            //查出来后将下标分页数据修改一下
            pageNo += total;
            //持久化到数据库中
            pointsBoardService.saveBatch(data);
        }
        //操作完后将ThreadLocal中的数据清空
        TableInfoContext.remove();
    }

    @SuppressWarnings("all")
    @XxlJob("clearPointsBoardFromRedis")
    public void clearPointsBoardFromRedis(){
        //清空redis中上个月的积分和排名数据
        //1.先获取上个月该天的日期时间
        LocalDate format = LocalDate.now().minusMonths(1);
        //2.组合key
        String key = RedisConstant.POINTS_BOARD_KEY_PREFIX + format.toString();
        //3.删除key
        redisTemplate.unlink(key);
    }
}
